<template>
  <el-dialog title="新增用户" :visible.sync="dialogVisible" width="30%" :append-to-body="true">
    <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="用户名" prop="uname">
        <el-input v-model="form.uname"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="form.role_ids" multiple style="width:100%">
          <el-option v-for="item in data" :key="item.kid" :label="item.name" :value="item.kid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input v-model="form.pwd" show-password></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('addForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { baseUrl } from "@/config/env";
const urls = {
  checkField: baseUrl + "/zweixin/common/check_unique", // 验证编码唯一性
  save: baseUrl + "/zweixin/sys/user/add",
  list: baseUrl + "/zweixin/sys/role/list_data"
};
export default {
  data() {
    return {
      dialogVisible: false,
      data: [],
      form: {
        name: null,
        uname: null,
        mobile: null,
        pwd: null,
        depart_id: null,
        role_ids: []
      },
      rules: {
        uname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              axios
                .get(urls.checkField, {
                  params: { table: "t_user", field: "uname", value: value }
                })
                .then(res => {
                  if (res.data.result) {
                    callback("用户名必须唯一");
                  } else {
                    callback();
                  }
                });
            },
            trigger: "blur"
          }
        ],
        // name: [{ required: true, message: "请输入用户姓名", trigger: "blur" }],
        pwd: [{ required: true, message: "请输入登录密码", trigger: "blur" }]
      }
    };
  },
  components: {},
  methods: {
    show(depart_id) {
      this.form.depart_id = depart_id;
      this.dialogVisible = true;
      if (this.$refs["addForm"]) {
        this.form.role_ids = [];
        this.$refs["addForm"].resetFields();
      }
      this.fetch();
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.form.role_ids = this.form.role_ids.toString();
          axios.post(urls.save, this.form).then(res => {
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
        }
        return false;
      });
    },
    fetch() {
      axios.get(urls.list).then(res => {
        this.data = res.data.result;
      });
    }
  }
};
</script>